#!/usr/bin/env groovy
package com.yidao.www

def check(project, arch, branch, distro, global){

    println '===================================='
    println 'distro: ' + distro
    println 'global: ' + global
    def robotBuildProfile = robotBuildProfile(project, arch, branch, distro, BUILD_NUMBER)
    def gitURL = robotBuildProfile.gitUrl.get(arch)[0].url

    def codeStyleProfile = codeStyleProfile()
    def projectReference = codeStyleProfile.get(project)
    println projectReference
    def label = projectReference.label
    def referenceJobName = projectReference.referenceJobName
    def checkTool = projectReference.checkTool
    def reportFile = projectReference.reportFile
    println '===================================='
    println 'label: ' + label
    println 'referenceJobName: ' + referenceJobName
    println 'checkTool: ' + checkTool
    println 'reportFile: ' + reportFile
    println '===================================='

    def base = new base()
    def gitScript = new gitScript()
    def checkCommitFiles
    def checkStatus
    // def global = true

    node(label){

        stage('checkout'){
            if(global){
                base.submoduleCheckout(gitURL, branch)
            }else{
                base.submoduleGerritCheckout(gitURL, branch)
            }
        }

        stage('checkCommitFiles'){
            checkCommitFiles = gitScript.checkGitCommitFiles()
            println 'checkCommitFiles: ' + checkCommitFiles
        }

        stage('checkStyle'){
            if(global){
                checkStatus = base.cpplintCheck(WORKSPACE, reportFile)
            }else{
                checkStatus = base.cpplintCheck(checkCommitFiles, reportFile)
            }
        }

        stage('recordIssues'){
            if(global){
                recordIssues(tools: [cppLint(pattern: reportFile, reportEncoding: 'UTF-8')])
            }else{
                recordIssues referenceJobName: referenceJobName, tools: [cppLint(pattern: reportFile, reportEncoding: 'UTF-8')]
            }
            base.archive(reportFile)
        }

        stage('queryNewIssues'){
            def result = base.queryNewIssues(BUILD_URL)
            def issues = result.issues
            def issueSize = result.size
            if(issueSize > 0){
                println '================================================='
                println '本次提交代码异常数量:' + issueSize
                println '================================================='
                println '本次提交代码异常列表:' + issues
            }else{
                println '================================================='
                println '本次提交无异常代码'
            }
        }

    }
}